package com.umeng.soexample.mvp.persenter;

import com.umeng.lib_core.mvp.presenter.BasePresenter;
import com.umeng.soexample.bean.SelUserEntity;
import com.umeng.soexample.bean.UpdateUserEntity;
import com.umeng.soexample.bean.UploadHeadEntity;
import com.umeng.soexample.mvp.contract.SelUserFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class SelUserFragmentPresenter extends BasePresenter<SelUserFragmentContract.SelUserFragmentIView, SelUserFragmentContract.SelUserFragmentIModel> {
    @Inject
    public SelUserFragmentPresenter(SelUserFragmentContract.SelUserFragmentIView selUserFramentIView, SelUserFragmentContract.SelUserFragmentIModel selUserFramentIModel) {
        super(selUserFramentIView, selUserFramentIModel);
    }

    public void selUserRequest(String userid){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userid", userid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.sel_user_request(jsonObject.toString()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SelUserEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(SelUserEntity entity) {
                        v.hideDialog();
                        v.SelUserSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

    public void updateUserRequest(String userid, String nickname, String usersex, String userbirthday, String userheight, String userweight){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userid", userid);
            jsonObject.put("nickname", nickname);
            jsonObject.put("usersex", usersex);
            jsonObject.put("userbirthday", userbirthday);
            jsonObject.put("userheight", userheight);
            jsonObject.put("userweight", userweight);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.update_user__request(jsonObject.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UpdateUserEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(UpdateUserEntity updateUserEntity) {
                        v.hideDialog();
                        v.UpdateUserSuccess(updateUserEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

    public void uploadHeadRequest(File uploadedfile, String userid){

        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), uploadedfile);
        builder.addFormDataPart("uploadedfile","TaoTao.jpg",body);
        builder.addFormDataPart("userid", userid);
        List<MultipartBody.Part> list = builder.build().parts();
        m.upload_head_request(list).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UploadHeadEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(UploadHeadEntity entity) {
                        v.hideDialog();
                        v.UploadHeadSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

}
